package com.yuzhitong.shapi.widget;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.yuzhitong.shapi.R;


/**
 * FileName: DownloadTextView
 * Author: brsya
 * Date: 2021/1/8 10:23
 * Description: 背景下载进度按钮
 */
public class DownloadTextView extends AppCompatTextView {
    private final static int DOWNLOAD_NO = 1;
    private final static int DOWNLOAD_LOADING = 1 << 1;
    private final static int DOWNLOAD_SUCCESS = 1 << 2;
    private final static int DOWNLOAD_OPEN = 1 << 3;


    private final static int COLOR_RED = 0b111111110000000000000000;
    private final static int COLOR_GREEN = 0b000000001111111100000000;
    private final static int COLOR_BLUE = 0b000000000000000011111111;


    private float blockLineWith;

    private int colorProgress;
    private boolean startDrawLine;
    private int progressPosition;

    private RectF backRect;
    private RectF progressRect;
    private Paint paint;
    private PorterDuffXfermode porterDuffXfermode;

    private int downloadType = DOWNLOAD_NO;
    private int roundSize = -1;

    private ObjectAnimator objectAnimator;
    private ObjectAnimator objectAnimatorY;


    public DownloadTextView(@NonNull Context context) {
        this(context, null);
    }

    public DownloadTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DownloadTextView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        progressRect = new RectF();
        progressRect.left = 0;
        progressRect.top = 0;

        backRect = new RectF();
        blockLineWith = getResources().getDisplayMetrics().density * 2;

        colorProgress = 0xFF2F8CFF;

        paint = new Paint();
        paint.setColor(colorProgress);
        //设置它的填充方法，用的多的是FILL 和 STORKE
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
    }

    public int getColorProgress() {
        return colorProgress;
    }

    public void setColorProgress(int colorProgress) {
        paint.setColor(colorProgress);
        this.colorProgress = colorProgress;
    }

    public int getProgressPosition() {
        return progressPosition;
    }

    public boolean isStartDrawLine() {
        return startDrawLine;
    }

    public void setStartDrawLine(boolean startDrawLine) {
        this.startDrawLine = startDrawLine;
        invalidate();
    }

    public int getDownloadType() {
        return downloadType;
    }

    public void setProgressPosition(int progressPosition) {
        downloadType = DOWNLOAD_LOADING;
        this.progressPosition = progressPosition;
        String progressText = progressPosition + "%";
        setText(progressText);
        setTextColor(Color.WHITE);
        invalidate();
    }

    public void setDownloadNoText(String text) {
        downloadType = DOWNLOAD_NO;
        setText(text);
        setTextColor(Color.WHITE);
        invalidate();
    }

    public int getRoundSize() {
        return roundSize;
    }

    public void setRoundSize(int roundSize) {
        this.roundSize = roundSize;
    }


    public void setDownloadSuccessText(String text) {
        downloadType = DOWNLOAD_SUCCESS;
        setText(text);
//        setTextColor(Color.WHITE);
        setTextColor(colorProgress);
        invalidate();
    }

    public void setDownloadOpenText(String text) {
        downloadType = DOWNLOAD_OPEN;
        setText(text);
        setTextColor(Color.WHITE);
        invalidate();
    }

    public void startAnimator(){
        if (objectAnimator != null && objectAnimator.isRunning()) {
            objectAnimator.cancel();
        }
        objectAnimator = ObjectAnimator.ofFloat(this, "scaleX", 1, .9f);
        objectAnimator.setDuration(400);
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
        objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
        objectAnimator.start();
        if (objectAnimatorY != null && objectAnimatorY.isRunning()) {
            objectAnimatorY.cancel();
        }
        objectAnimatorY = ObjectAnimator.ofFloat(this, "scaleY", 1, .9f);
        objectAnimatorY.setDuration(400);
        objectAnimatorY.setInterpolator(new LinearInterpolator());
        objectAnimatorY.setRepeatCount(ValueAnimator.INFINITE);
        objectAnimatorY.setRepeatMode(ValueAnimator.REVERSE);
        objectAnimatorY.start();
    }

    public void stopAnimator(){

        if (objectAnimator != null){
            objectAnimator.cancel();
        }

        if (objectAnimatorY != null){
            objectAnimatorY.cancel();
        }

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        backRect.left = 0;
        backRect.top = 0;
        backRect.right = right - left;
        backRect.bottom = bottom - top;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int drawRound = getWidth() / 2;
        if (roundSize >= 0 && roundSize <= getWidth() / 2){
            drawRound = roundSize;
        }
        switch (downloadType) {
            default:
            case DOWNLOAD_NO:
                progressRect.top = 0;
                progressRect.left = 0;
                progressRect.right = getWidth();
                progressRect.bottom = getHeight();
                paint.setColor(colorProgress);
                canvas.drawRoundRect(backRect, drawRound, drawRound, paint);
                paint.setXfermode(null);
                break;
            case DOWNLOAD_LOADING:
                progressRect.top = 0;
                progressRect.left = 0;
                progressRect.right = (int) (getWidth() * progressPosition / 100);
                progressRect.bottom = getHeight();
                paint.setColor(colorProgress);
                canvas.drawRoundRect(backRect, drawRound, drawRound, paint);
                paint.setColor(Color.argb(66, 255, 255, 255));
                canvas.drawRoundRect(backRect, drawRound, drawRound, paint);
                paint.setXfermode(porterDuffXfermode);
                paint.setColor(colorProgress);
                canvas.drawRect(progressRect, paint);
                paint.setXfermode(null);
                break;
            case DOWNLOAD_SUCCESS:
//                progressRect.top = 0;
//                progressRect.left = 0;
//                progressRect.right = getWidth();
//                progressRect.bottom = getHeight();
////                paint.setColor(Color.argb(0xFF, (colorProgress & COLOR_RED) >> 1, (colorProgress & COLOR_GREEN) >> 1, (colorProgress & COLOR_BLUE) >> 1));
//                int r = (colorProgress & COLOR_RED);
//                int g = (colorProgress & COLOR_GREEN);
//                int b = (colorProgress & COLOR_BLUE);
//                int rgbMax = Math.max(r, Math.max(g, b));
//                int rgbAdd = 255 - rgbMax;
//                if (rgbAdd == 0) {
//                    rgbAdd = (255 * 3 - (r + g + b)) / 3;
//                }
//                paint.setColor(Color.argb(0xFF, r == rgbMax ? r : r + rgbAdd, g == rgbMax ? g : g + rgbAdd, b == rgbMax ? b : b + rgbAdd));
////                paint.setColor(0xFF999999);
//                canvas.drawRoundRect(progressRect, (int) (getWidth() / 2), (int) (getWidth() / 2), paint);
//                paint.setXfermode(null);
                // 幽灵按钮
                paint.setColor(colorProgress);
                canvas.drawRoundRect(backRect, drawRound, drawRound, paint);
                progressRect.top = blockLineWith;
                progressRect.left = blockLineWith;
                progressRect.right = getWidth() - blockLineWith;
                progressRect.bottom = getHeight() - blockLineWith;
                paint.setColor(Color.WHITE);
                canvas.drawRoundRect(progressRect, drawRound, drawRound, paint);
                paint.setXfermode(null);
                break;
            case DOWNLOAD_OPEN:
                progressRect.top = 0;
                progressRect.left = 0;
                progressRect.right = getWidth();
                progressRect.bottom = getHeight();
//                paint.setColor(Color.argb(0xFF, (colorProgress & COLOR_RED) >> 1, (colorProgress & COLOR_GREEN) >> 1, (colorProgress & COLOR_BLUE) >> 1));
                paint.setColor(0xFF999999);
                canvas.drawRoundRect(progressRect, drawRound, drawRound, paint);
                paint.setXfermode(null);
//                paint.setColor(colorProgress);
//                canvas.drawRoundRect(backRect, (int) (getWidth() / 2), (int) (getWidth() / 2), paint);
//                progressRect.top = blockLineWith;
//                progressRect.left = blockLineWith;
//                progressRect.right = getWidth() - blockLineWith;
//                progressRect.bottom = getHeight() - blockLineWith;
//                paint.setColor(Color.WHITE);
//                canvas.drawRoundRect(progressRect, (int) (getWidth() / 2), (int) (getWidth() / 2), paint);
//                paint.setXfermode(null);
                break;
        }
        super.onDraw(canvas);
    }
}
